#保存实时分析的分贝数据
import time
from utils.StandardSqlite3DB import SQLiteDB
import colorama
from colorama import Fore, Style

# 初始化 colorama
colorama.init(autoreset=True)


def test(message,SAVE_SECONDS):
    while True:
        time.sleep(SAVE_SECONDS)
        print(message.qsize())
        while True:
            try:
                m = message.get_nowait()
                print("save test ", m, message.qsize())
            except:
                print("队列为空，等待...")
                break  # 队列为空时退出
        #return items


#保存为本地文件
def save_localfile(message,SAVE_SECONDS):
    while True:
        time.sleep(SAVE_SECONDS)
        print(message.qsize())
        data_str = ''
        while True:
            try:
                m = message.get_nowait()
                #print("##", m, message.qsize())
                data_str += str(m)+'\n'
            except:
                print("队列为空，等待...")
                break  # 队列为空时退出

        with open("./log.txt", "a") as fil1:  # 相对路径打开文件
            fil1.write(data_str)
        print("保存完成")


#保存数据库sqlite3
def save_to_sqlite3(message,table_name,SAVE_SECONDS):
    db = SQLiteDB('data/fenbei_monitor.db')
    while True:
        time.sleep(SAVE_SECONDS)
        print(message.qsize())
        list_data = []
        while True:
            try:
                m = message.get_nowait()
                list_data.append(m)
            except:
                print(f"\033[32m 队列为空，等待... \033[0m")
                break  # 队列为空时退出
        db.insert_many(table_name,list_data)






if __name__ == '__main__':
    pass
